 package com.oanda.blackberry.restapi;
 
 import com.oanda.blackberry.net.ConnectionManager;
 import com.oanda.blackberry.net.RestException;
 import com.oanda.blackberry.net.UrlEncoder;
 import com.oanda.blackberry.util.log.Logger;
 import java.io.IOException;
 import net.rim.device.api.util.Persistable;
 
 public class Account extends Rest
   implements Persistable
 {
   AccountEntry[] accountEntrys;
 
   public String list(String sessionToken, String username)
     throws RestException
   {
     String url = ConnectionManager.getUrl() + "v1/account/list.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + "&username=" + UrlEncoder.encode(username, "");
 
       url = url + parameters;
     } catch (IOException io) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("account list response: " + this.response);
     return this.response;
   }
 
   public void setAccountEntrys(AccountEntry[] accountEntrys)
   {
     sortAccountEntries(accountEntrys);
     this.accountEntrys = accountEntrys;
   }
 
   public AccountEntry[] getAccountEntrys() {
     return this.accountEntrys;
   }
 
   private void sortAccountEntries(AccountEntry[] entries) {
     if (entries.length == 1) {
       return;
     }
 
     for (int i = 0; i < entries.length; i++)
       for (int j = i + 1; j < entries.length; j++)
       {
         if ((entries[j].getName().indexOf("Primary") > -1) && (entries[j].getName().compareTo(entries[i].getName()) < 0)) {
           AccountEntry tmp = entries[i];
           entries[i] = entries[j];
           entries[j] = tmp;
         }
       }
   }
 
   public String status(String accountId, String sessionToken)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       String text = null;
       if (accountId.equals("1")) {
         text = "{\"account_id\":1,\"balance\":99967.16,\"unrealized_pl\":1629.21,\"nav\":101660.07,\"realized_pl\":-36830.09,\"margin_used\":3160.62,\"margin_avail\":98499.45,\"open_trades\":10,\"open_orders\":7}";
       }
       else if (accountId.equals("2")) {
         text = "{\"account_id\":2,\"balance\":5967.16,\"unrealized_pl\":9.1,\"nav\":10.07,\"realized_pl\":70.09,\"margin_used\":0.62,\"margin_avail\":99.45,\"open_trades\":7,\"open_orders\":8}";
       }
       else if (accountId.equals("3")) {
         text = "{\"account_id\":3,\"balance\":967.16,\"unrealized_pl\":19.21,\"nav\":101660.07,\"realized_pl\":-830.09,\"margin_used\":160.62,\"margin_avail\":499.45,\"open_trades\":3,\"open_orders\":2}";
       }
 
       return text;
     }
 
     String url = ConnectionManager.getUrl() + "v1/account/status.json";
     try
     {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + "&account_id=" + accountId;
 
       url = url + parameters;
     } catch (IOException i) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("account list response: " + this.response);
     return this.response;
   }
}
